<?php

namespace App\Admin\Forms\User;

use App\Exceptions\AdminInvalidRequestException;
use App\Models\User;
use Carbon\Carbon;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Http\JsonResponse;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Godruoyi\Snowflake\Snowflake;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class ReviewForm extends Form implements LazyRenderable
{
    use LazyWidget;

    private int $expire = 30;
    private int $state;
    private string $remark;
    private string $key;
    private  $payment;

    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return JsonResponse
     * @throws AdminInvalidRequestException
     */
    public function handle(array $input): JsonResponse
    {
        $ids = explode(',', $input['ids'] ?? null);
        if (!$ids) {
            throw new AdminInvalidRequestException(trans('error.400'));
        }

        //$this->state = $input['state'];
        //$data_insert = ['state'=>$input['state']];
        //if($input['state'] == 4){
        //    $data_insert['shop_created_at'] = now();
        //}
        foreach ($ids as $id){
            $user = User::where('id','=',$id)->first();
            $user->state = $input['state'];
            if($input['state'] == User::STATE_PASS){
                $user->shop_created_at = now();
            }
            if($input['state'] == User::STATE_FAIL){
                $user->denial_reason = $input['denial_reason'];
            }
            $user->save();
        }

        return $this
            ->response()
            ->success(trans('admin.succeeded'))
            ->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {

        $this->radio('state', trans('user.fields.state'))->options([
            User::STATE_PASS => '通过',
            User::STATE_FAIL => '拒绝',
        ])->required()->when(User::STATE_FAIL ,function (Form $form) {
            $this->text('denial_reason', trans('user.fields.denial_reason'));
        });

        $this->hidden('ids')->attribute('id', 'batch-ids');
    }


}
